//
//  TabBarController.m
//  gdemm
//
//  Created by 乔明超 on 16/9/20.
//  Copyright © 2016年 Goldencis. All rights reserved.
//

#import "TabBarController.h"
#import "NavigationController.h"
#import "AlertsController.h"
#import "CloudDiskController.h"
#import "Utils.h"
#import "ExamineViewController.h"
#import "SafetyDesktopViewController.h"
#import "AppStoreController.h"
@interface TabBarController ()<UITabBarControllerDelegate>

@end

@implementation TabBarController
+ (void)initialize{
    NSDictionary *textAtt = @{NSForegroundColorAttributeName:[Utils UIColorFromHex:0Xffffff],NSFontAttributeName:[UIFont systemFontOfSize:12]};

    NSDictionary *textAttSelected = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    [[UITabBarItem appearance]setTitleTextAttributes:textAtt forState:UIControlStateNormal];
    [[UITabBarItem appearance]setTitleTextAttributes:textAttSelected forState:UIControlStateSelected];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(skinSwitchHandler) name:@"skinswitch" object:nil];
    self.tabBar.translucent = NO;
    self.tabBar.itemPositioning = UITabBarItemPositioningFill;
    self.tabBar.barTintColor = [Utils colorWithKey:@"mainBgColor"];
   //添加子控制器
    
    [self setupChildVc:[[SafetyDesktopViewController  alloc]init] title:NSLocalizedString(@"str_safety_Desktop",@"安全桌面") image:@"tabBar_desktop_icon" selectedImage:@"tabBar_desktop_click_icon"];
    
    [self setupChildVc:[[ExamineViewController  alloc]init] title:NSLocalizedString(@"str_evaluation", @"测评") image:@"tabBar_evaluation_icon" selectedImage:@"tabBar_evaluation_click_icon"];
    [self setupChildVc:[[CloudDiskController alloc] init] title: NSLocalizedString(@"str_cloud_disk", @"云盘") image:@"tabBar_cloudDisk_icon" selectedImage:@"tabBar_cloudDisk_click_icon"];
    [self setupChildVc:[[AlertsController alloc] init] title:NSLocalizedString(@"str_alerts", @"消息") image:@"tabBar_alerts_icon" selectedImage:@"tabBar_alerts_click_icon"];
    [self setupChildVc:[[AppStoreController alloc] init] title:NSLocalizedString(@"str_appStore", @"应用商店") image:@"tabBar_appstore_icon" selectedImage:@"tabBar_appstore_click_icon"];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self hiddenTopLine];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
}
/**
 * 初始化子控制器
 */
- (void)setupChildVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 设置文字和图片
    vc.navigationItem.title = title;
    vc.tabBarItem.title = title;
    UIImage * img = [Utils getImageByDevce:image];
    vc.tabBarItem.image =  [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage * selectedImg = [Utils imageWithKey:selectedImage];
    vc.tabBarItem.selectedImage =   [selectedImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 包装一个导航控制器, 添加导航控制器为tabbarcontroller的子控制器
    NavigationController *nav = [[NavigationController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
    
}

- (void)hiddenTopLine{
    CGRect rect = CGRectMake(0, 0, kScreenWidth, 0.5);    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();
    [self.tabBar setBackgroundImage:img];
    [self.tabBar setShadowImage:img];

}

/**
 *  肤色切换
 */

- (void)skinSwitchHandler{
    self.tabBar.barTintColor = [Utils colorWithKey:@"mainBgColor"];
    
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    UINavigationController * nav = (UINavigationController *)viewController;
    if ([[nav.topViewController class] isSubclassOfClass:[AppStoreController class]]) {
        AppStoreController *appStoreVc = (AppStoreController *)  nav.topViewController;
        if (appStoreVc.searchtextFiled) {
             [appStoreVc loadAppInfoList:false];
        }
       
    }
}


@end
